package org.adorsys.aderp.cash.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.adorsys.aderp.cash.domain.CashRegister;
import org.adorsys.aderp.cash.work.cashstate.BasicCashRegStateMaintainerUtil;
import org.adorsys.aderp.cash.work.cashstate.BasicFullDayCashRegister;
import org.adorsys.aderp.cash.work.cashstate.CashRegStateMaintainer;
import org.adorsys.aderp.cash.work.cashstate.FullDayCashRegister;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RequestMapping("/cashregisters")
@Controller
@RooWebScaffold(path = "cashregisters", formBackingObject = CashRegister.class)
public class CashRegisterController {
	@Resource
	private volatile CashRegStateMaintainer cashRegStateMaintainer;
	
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public java.lang.String show(@PathVariable("id") java.lang.Long id, Model uiModel) {
    	CashRegister cashRegister = cashRegisterService.findCashRegister(id);
    	List<FullDayCashRegister> fullDayCashRegisters = cashRegStateMaintainer.getFullDayCashRegisters();
    	FullDayCashRegister fullDayCashRegister = BasicCashRegStateMaintainerUtil.getFullDayCashRegister(cashRegister, fullDayCashRegisters);
    	System.out.println("Full day cash register "+fullDayCashRegister);
        uiModel.addAttribute("cashregister",cashRegister );
        uiModel.addAttribute("itemId", id);
        uiModel.addAttribute("fullDayCashRegister", fullDayCashRegister);
        return "cashregisters/show";
    }
    @RequestMapping(value="/open/{id}",method=RequestMethod.GET)
    public String open(@PathVariable("id") Long id,Model uiModel, HttpServletRequest httpServletRequest){
    	CashRegister cashRegister = cashRegisterService.findCashRegister(id);
    	BasicFullDayCashRegister basicFullDayCashRegister = new BasicFullDayCashRegister(cashRegister);
		cashRegStateMaintainer.open(basicFullDayCashRegister);
		BasicCashRegStateMaintainerUtil.openFullDayCash(basicFullDayCashRegister);
		return "redirect:/cashregisters/" + encodeUrlPathSegment(cashRegister.getId().toString(), httpServletRequest);
    }
    @RequestMapping(value="/close/{id}",method=RequestMethod.GET)
    public String close(@PathVariable("id") Long id,Model uiModel, HttpServletRequest httpServletRequest){
    	CashRegister cashRegister = cashRegisterService.findCashRegister(id);
    	BasicFullDayCashRegister basicFullDayCashRegister = new BasicFullDayCashRegister(cashRegister);
		cashRegStateMaintainer.close(basicFullDayCashRegister);
		BasicCashRegStateMaintainerUtil.closeFullDayCash(basicFullDayCashRegister);
		return "redirect:/cashregisters/" + encodeUrlPathSegment(cashRegister.getId().toString(), httpServletRequest);
    }

    @RequestMapping(value="/suspend/{id}",method=RequestMethod.GET)
    public String suspend(@PathVariable("id") Long id,Model uiModel, HttpServletRequest httpServletRequest){
    	CashRegister cashRegister = cashRegisterService.findCashRegister(id);
    	BasicFullDayCashRegister basicFullDayCashRegister = new BasicFullDayCashRegister(cashRegister);
		cashRegStateMaintainer.suspend(basicFullDayCashRegister);
		BasicCashRegStateMaintainerUtil.suspendFullDayCash(basicFullDayCashRegister);
		return "redirect:/cashregisters/" + encodeUrlPathSegment(cashRegister.getId().toString(), httpServletRequest);
    }
}
